<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>nagios调用短信接口 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>nagios调用短信接口</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nagios/">Nagios</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;公司正好有自己的短信接口平台，接下来带大家一起配置nagios调用第三方短信接口。</p>
<ol>
<li>首先我们要写一个调用短信接口的脚本，网上的脚本大都是python写的，我这个是shell写的，比较好理解。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi /root/duanxin.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 脚本的日志文件 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">LOGFILE=<span class="string">"/data1/sms_log/sms_send_.log"</span>  <span class="comment">#定义发送短信的日志信息 文件</span></div><div class="line">:&gt;&gt;<span class="string">"<span class="variable">$LOGFILE</span>"</span> </div><div class="line"><span class="built_in">exec</span> 1&gt;&gt;<span class="string">"<span class="variable">$LOGFILE</span>"</span> </div><div class="line"><span class="built_in">exec</span> 2&gt;&amp;1 </div><div class="line"></div><div class="line">Uid=<span class="string">"test"</span>    <span class="comment">#接口的用户名，这个使用接口时对方会提供，我这里的test是随意写的</span></div><div class="line">Key=<span class="string">"123456"</span>  <span class="comment">#密码与用户名对应，也是接口方提供</span></div><div class="line"></div><div class="line">MOBILE_NUMBER=<span class="variable">$1</span> <span class="comment"># 接受短信的手机号码  </span></div><div class="line">QIANMING=<span class="string">"%e3%80%90%e9%a9%ac%e5%8f%af%e6%b3%a2%e7%bd%97%e7%bd%91%e3%80%91"</span> <span class="comment">#这里重点说一下，签名有的接口需要，有的不需要，因为我们公司的接口需要，所以需要添加上，我这里的签名内容是经过编码的，不加编码会导致发送失败，具体工作中需不需要编码还得看接口哪边有没有要求。</span></div><div class="line">XXD=<span class="string">"/usr/bin/xxd"</span> </div><div class="line">CURL=<span class="string">"/usr/bin/curl"</span> </div><div class="line">TIMEOUT=5 </div><div class="line">MESSAGE_ENCODE=$(<span class="built_in">echo</span> $(/usr/<span class="built_in">local</span>/bin/php -r <span class="string">"echo urlencode(\"<span class="variable">$2</span>\");"</span>; ) )  <span class="comment">#这里的$2是nagios发送短信的第二个变量</span></div><div class="line">URL=<span class="string">"http://192.168.100.100:8888/services/msgsend.asmx/SendMsg?userCode=<span class="variable">$&#123;$Uid&#125;</span>&amp;userPass=<span class="variable">$&#123;Key&#125;</span>&amp;DesNo=<span class="variable">$&#123;MOBILE_NUMBER&#125;</span>&amp;Msg=<span class="variable">$&#123;MESSAGE_ENCODE&#125;</span><span class="variable">$&#123;QIANMING&#125;</span>&amp;Channel=0"</span></div><div class="line"><span class="comment">#这里的URL是胡乱写的，不可能暴漏自己公司的接口哈，但是格式大体是这样的，到时候接口方会提供URL的格式的</span></div><div class="line"><span class="comment"># Send it </span></div><div class="line"><span class="built_in">set</span> -x </div><div class="line"><span class="variable">$&#123;CURL&#125;</span> -s --connect-timeout <span class="variable">$&#123;TIMEOUT&#125;</span> <span class="string">"<span class="variable">$&#123;URL&#125;</span>"</span></div></pre></td></tr></table></figure>
<ol>
<li>测试脚本</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">bash /root/duanxin.sh <span class="string">"手机号"</span> “内容”</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果脚本报错，可以根据报错信息检查脚本，如果脚本没有问题，但是短信发不出去，可以看看sms_send_log里面的报错信息</p>
<ol>
<li>nagios 调用脚本，不要忘记脚本要给执行权限，一般脚本放在root目录下，nagios在调用脚本时是不能访问root目录的，所以你还要看你/root目录的权限</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">define <span class="built_in">command</span> &#123;</div><div class="line">       command_name host-notify-by-sms</div><div class="line">       command_line /root/duanxin.sh <span class="variable">$CONTACTPAGER</span>$ <span class="string">"<span class="variable">$HOSTNAME</span>$ <span class="variable">$HOSTSTATE</span>$ <span class="variable">$SHORTDATETIME</span>$"</span></div><div class="line">       &#125;</div><div class="line"></div><div class="line">define <span class="built_in">command</span> &#123;</div><div class="line">       command_name service-notify-by-sms</div><div class="line">       command_line /root/duanxin.sh <span class="variable">$CONTACTPAGER</span>$ <span class="string">"<span class="variable">$SERVICESTATE</span>$ <span class="variable">$SERVICEOUTPUT</span>$ <span class="variable">$HOSTALIAS</span>$/<span class="variable">$SERVICEDESC</span>$ <span class="variable">$SHORTDATETIME</span>$"</span></div><div class="line">       &#125;</div></pre></td></tr></table></figure>
<ol>
<li>看到这里大家可能对上面脚本的$1和$2概念比较模糊，其实刚开始我也迷糊，nagios怎么知道我要发送的号码呢，后来研究发现，$CONTACTPAGER$这个<br>量就是nagios内部联系人的变量，也就是他会调用我们在contacts.cfg里面定义的手机号，而我们脚本里面定义的$1就对应$CONTACTPAGER$，$2就对应”$HOSTNAME$ $HOSTSTATE$ $SHORTDATETIME$” </li>
</ol>

	

	
		<span class="different-posts"><a href="/2017/10/12/Nagios/5. nagios调用短信接口/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
